var express = require('express');
var router = express.Router();

var userDao = require('../dao/userDao');

/* GET users listing. */
router.get('/', function(req, res, next) {
    res.send('respond with a resource');
});

// 增加用户
//TODO 同时支持get,post
router.get('/addUser', function(req, res, next) {
    res.render('add');
});

router.post('/addUser', function(req, res, next) {
    userDao.add(req, res, next,function(result){
        res.render('success',{'result':result});
    });
});

//查询所有
router.get('/queryAll', function(req, res, next) {
    userDao.queryAll(req, res, next,function(userList,pageNum,userListSize){
        //console.log(userList);
        res.render('index', {"userList": userList,"pageNum":pageNum,"userListSize":userListSize});
    });
});

//通过id查询
router.get('/query', function(req, res, next) {
    userDao.queryById(req, res, next,function(user){
        res.render("detial",{"user":user});
    });
});

//删除用户
router.get('/deleteUser', function(req, res, next) {
    userDao.delete(req, res, next);
});

//通过id查询修改
router.get('/queryForUpdate', function(req, res, next) {
    userDao.queryById(req, res, next,function(user){
        res.render("update",{"user":user});
    });
});

//修改用户
router.post('/updateUser', function(req, res, next) {
    userDao.update(req, res, next,function(result){
        // 使用页面进行跳转提示
        if(result.affectedRows > 0) {
            res.render('success', {result: result});
        } else {
            res.render('fail',  {result: result});
        }
    });
});

module.exports = router;